(function () {
    var $pageBar = null;
    var _pageSize = 10;
    var _pageIndex = 1;

    function bundlingEvent(callback) {
        //点击页码
        $pageBar.on('click', '.e-pagenum', function () {
            var pageSize = $pageBar.find('input[name=pageSize]').val();
            var pageNum = $(this).text();
            $pageBar.find('input[name=pageIndex]').val(pageNum);
            callback && callback(pageNum, pageSize);
        });
        //首页
        $pageBar.on('click', '.e-first-page', function () {
            var pageSize = $pageBar.find('input[name=pageSize]').val();
            var pageNum = _pageIndex;
            $pageBar.find('input[name=pageIndex]').val(pageNum);
            callback && callback(pageNum, pageSize);
        });
        //尾页
        $pageBar.on('click', '.e-last-page', function () {
            var pageSize = $pageBar.find('input[name=pageSize]').val();
            var pageNum = $pageBar.find('input[name=totalPageNum]').val();
            $pageBar.find('input[name=pageIndex]').val(pageNum);
            callback && callback(pageNum, pageSize);
        });
        //下一页
        $pageBar.on('click', '.e-next-page', function () {
            var pageSize = $pageBar.find('input[name=pageSize]').val();
            var totalPageNum = $pageBar.find('input[name=totalPageNum]').val();
            var currPageNum = $pageBar.find('.pagebar_numlist_current').text();
            var pageNum = parseInt(currPageNum) + 1 > totalPageNum ? totalPageNum : parseInt(currPageNum) + 1;
            if (isNaN(pageNum)) {
                pageNum = _pageIndex;
            }
            $pageBar.find('input[name=pageIndex]').val(pageNum);
            callback && callback(pageNum, pageSize);
        });
        //上一页
        $pageBar.on('click', '.e-up-page', function () {
            var pageSize = $pageBar.find('input[name=pageSize]').val();
            var currPageNum = $pageBar.find('.pagebar_numlist_current').text();
            var pageNum = currPageNum - _pageIndex < _pageIndex ? _pageIndex : currPageNum - _pageIndex;
            if (isNaN(pageNum)) {
                pageNum = _pageIndex;
            }
            $pageBar.find('input[name=pageIndex]').val(pageNum);
            callback && callback(pageNum, pageSize);
        });
        $pageBar.on('click', '.btn', function () {

            var pageNum = parseInt($pageBar.find('input[name=pageIndex]').val());
            var totalPageNum = parseInt($pageBar.find('input[name=totalPageNum]').val());
            var pageSize = parseInt($pageBar.find('input[name=pageSize]').val());
            if (isNaN(pageNum) || pageNum < _pageIndex) {
                pageNum = _pageIndex;
            } else if (pageNum > totalPageNum) {
                pageNum = totalPageNum;
            }
            if (isNaN(pageSize)) {
                pageSize = _pageSize;
            }
            $pageBar.find('input[name=pageIndex]').val(pageNum);
            callback && callback(pageNum, pageSize);
        })
    }


    var page = {
        init: function (total, pageNum, pageSize, pageBar, callback) {
            $pageBar = (pageBar instanceof jQuery) ? pageBar : $('#' + pageBar);
            if (total < _pageIndex) {
                $pageBar.html("无数据");
                return;
            }

            var totalPageNum = total % pageSize > 0 ? parseInt(total / pageSize) + _pageIndex : parseInt(total / pageSize);

            pageNum = pageNum + _pageIndex;
            var num = parseInt(total / pageSize) + ((total % pageSize) ? _pageIndex : 0);
            var begin = _pageIndex, end = num;
            if (num > 5 && (pageNum + 4) <= num) {
                begin = pageNum < 3 ? _pageIndex : pageNum - 2;
                end = begin + 4;
            } else {
                if (num <= 5) {
                    begin = _pageIndex;
                } else {
                    begin = num - 4;
                }
            }
            var htmlContent = '<p class="pagebar_numlist">';
            htmlContent += '<input type="hidden" name="totalPageNum" value="' + totalPageNum + '"/>';
            htmlContent += '<input type="hidden" name="pageIndex" value="' + pageNum + '"/>';
            htmlContent += '<input type="hidden" name="pageSize" value="' + _pageSize + '"/>';
            if (pageNum == _pageIndex) {
                htmlContent += '<a title="上一页" class="g-pageprev e-up-page disable" href="javascript:void(0);" >上一页</a>';
            } else {
                htmlContent += '<a title="上一页" class="g-pageprev e-up-page" href="javascript:void(0);" >上一页</a>';
            }
            htmlContent += '<a class="pagebar_numlist_first e-first-page" title="首页" href="javascript:void(0)">&nbsp;首页&nbsp;</a>';
            if (begin > _pageIndex) {
                htmlContent += "<a class='dot'>...</a>";
            }
            for (var i = begin; i <= end; i++) {
                if (pageNum == i) {
                    htmlContent += '<span class="pagebar_numlist_current">' + i + '</span>';
                } else {
                    htmlContent += '<a title="第' + i + '页" href="javascript:void(0);" class="e-pagenum">' + i + '</a>';
                }
            }
            if (end < num) {
                htmlContent += '<a class="dot">...</a>';
            }
            htmlContent += '<a class="pagebar_numlist_last e-last-page" title="尾页" href="javascript:void(0);">&nbsp;尾页&nbsp;</a>';
            if (pageNum == num) {
                htmlContent += '<a title="下一页" class="g-pagenext e-next-page disable" href="javascript:void(0);">下一页 ></a>';
            } else {
                htmlContent += '<a title="下一页" class="g-pagenext e-next-page" href="javascript:void(0);">下一页</a>';
            }
            htmlContent += '<span class="btn-text"><em>共' + total + '条</em>';
            htmlContent += '</p>';
            $pageBar.html(htmlContent);

            bundlingEvent(callback);
        },
        clear: function (pageBar) {
            $pageBar = (pageBar instanceof jQuery) ? pageBar : $('#' + pageBar);
            $pageBar.empty();
        }
    }

    window.page = page;
})();